package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.result.PageResult;
import com.itheima.mapper.CheckgroupCheckitemMapper;
import com.itheima.mapper.CheckgroupMapper;
import com.itheima.pojo.dto.CheckgroupDTO;
import com.itheima.pojo.dto.CheckgroupPageQueryDTO;
import com.itheima.pojo.vo.CheckgroupVO;
import com.itheima.service.CheckgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 检查组Service业务层处理
 * 
 * @author hjc
 * @date 2025-01-11
 */
@Service
public class CheckgroupServiceImpl implements CheckgroupService
{
    @Autowired
    private CheckgroupMapper checkgroupMapper;

    @Autowired
    private CheckgroupCheckitemMapper checkgroupCheckitemMapper;

    /**
     * 检查组分页查询
     * @param checkgroupPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CheckgroupPageQueryDTO checkgroupPageQueryDTO) {
        PageHelper.startPage(checkgroupPageQueryDTO.getCurrentPage(),checkgroupPageQueryDTO.getPageSize());
        //判断前端传来的是编号还是名字，再set进dto里对应的code或name
        if(checkgroupPageQueryDTO.getQueryString() != null && checkgroupPageQueryDTO.getQueryString().contains("0")){
            checkgroupPageQueryDTO.setCode(checkgroupPageQueryDTO.getQueryString());
        }else{
            checkgroupPageQueryDTO.setName(checkgroupPageQueryDTO.getQueryString());
        }

        Page<CheckgroupVO> page = checkgroupMapper.pageQuery(checkgroupPageQueryDTO);
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增检查组
     * @param checkgroupDTO
     */
    @Override
    public void save(CheckgroupDTO checkgroupDTO,List<Long> checkitemIds) {
        checkgroupMapper.insertCheckgroup(checkgroupDTO);
        Long checkgroupId = Long.valueOf(checkgroupDTO.getId());
        for (Long checkitemId : checkitemIds) {
            checkgroupCheckitemMapper.insert(checkgroupId,checkitemId);
        }
    }
    /**
     * 删除检查组
     *
     * @param id 需要删除的检查组主键
     */

    @Transactional
    @Override
    public void delete(Long id) {
        try {
            // 先删除 t_checkgroup_checkgroup 表中相关的记录
            checkgroupCheckitemMapper.deleteByCheckgroupId(id);
            // 再删除 t_checkgroup 表中的记录
            checkgroupMapper.deleteCheckgroupById(id);
        } catch (Exception e) {
            // 处理异常，回滚事务
            throw new RuntimeException("删除检查组失败", e);
        }
    }


    @Override
    public CheckgroupDTO getById(Long id) {
        CheckgroupDTO checkgroupDTO = checkgroupMapper.getById(id);
        return checkgroupDTO;
    }

    /**
     * 修改检查组
     *
     * @param checkgroupDTO 检查组信息
     */
    @Override
    @Transactional
    public void update(CheckgroupDTO checkgroupDTO, List<Long> checkitemIds) {
        checkgroupMapper.update(checkgroupDTO);
        Long checkgroupId = Long.valueOf(checkgroupDTO.getId());
        checkgroupCheckitemMapper.deleteByCheckgroupId(checkgroupId);
        for (Long checkitemId : checkitemIds) {
            checkgroupCheckitemMapper.insert(checkgroupId,checkitemId);
        }
    }


}
